/**
 * Created by chentao on 2015/8/13.
 */

var ParkAdmin;
if (!ParkAdmin) {
    ParkAdmin = {}
}

//全部园区列表
var bindPark = function (dom) {
    $.post($().AbsUrlParse("/web/park/findallparks"), {}, function (res) {
        if (res.res.code == 40000) {
            var data = res.res.data;
            var htm = "";
            for (var i = 0; i < data.length; i++) {
                htm = htm + "<option value='" + data[i].id + "'>" + data[i].parkName + "</option>";
            }

            $(dom).html(htm);
            $(dom).multipleSelect({
                single: true,
                filter: true,
                width: '100%'
            });
            $(dom).change(function () {
                var name = $(this).children('option:selected').text();
                $("input[name=parkName]").val(name);
            });
        }
    });
};

/**
 * 显示状态
 * @param value
 * @param row
 * @param index
 * @returns {string}
 */
ParkAdmin.showEnable = function (value, row, index) {
    if (row.id != 1) {
        if (value == 1) {
            return [
                '<a href="javascript:void(0)" onclick="mergeEnable(' + 0 + ',' + row.id + ')"><span style="color: green">可用</span></a>'
            ].join('');
        } else {
            return [
                '<a href="javascript:void(0)" onclick="mergeEnable(' + 1 + ',' + row.id + ')"><span style="color: red">不可用</span></a>'
            ].join('');
        }
    }
};

/**
 * 操作
 * @param value
 * @param row
 * @param index
 * @returns {string}
 */
ParkAdmin.formatter = function (value, row, index) {
    return [
        '<a href="javascript:void(0)" onclick="showMergeModel(' + value + ')"><span style="color: red">重置密码</span></a>'
    ].join('');
};

/**
 * 修改用户状态
 * @param enable
 * @param id
 */
var mergeEnable = function (enable, id) {
    comment.post("/web/admin/mergeEnable", {id: id, enable: enable}, null, function (data) {
        if (data.res.code == 40000) {
            comment.refresh();
        } else {
            hint("操作失败");
        }
    });
    return false;
};

/**
 * 点击添加用户按钮
 */
bindClick("#addAdmin", function () {
    bindPark("#parkId");

    showModel("addAdmin-div", function () {
    });
});

//提交表单后回调
var saveAdmin = function (data) {
    var d = JSON.parse(data);
    console.log(d);
};

bindClick("#deleteAdmin", function () {
    comment.showOperationTips("确定删除吗？", function () {
        var rowid = $("#id").val();
        $.post($().AbsUrlParse('/web/ensadmin/delete'), {
            id: rowid
        }, function (data) {
            if (data.state == true) {
                if (data.res.code == 40000) {
                    hint("删除成功");
                    comment.refresh();
                    closeModel("editAdmin-div");
                } else {
                    hint(data.res.msg);
                }
            } else {
                hint(data.msg);
            }
        });
    });
});


ParkAdmin.editAdminShow = function (row, formid) {
    setRowToFrm(formid, row);
    return false;
};